import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

//用户端登录界面设计
public class UserInteractive extends JFrame {

    //标签设置
    //1
    JLabel lab1_1 = new JLabel("用户名: ");
    JLabel lab1_2 = new JLabel("密码: ");
    //2
    JLabel lab2_1 = new JLabel("请输入新的用户名: ");
    JLabel lab2_2 = new JLabel("请输入新用户密码: ");
    JLabel lab2_3 = new JLabel("请确认新用户密码: ");

    //文本框设置
    //1
    JTextField jt1_1 = new JTextField(12);
    JPasswordField jt1_2 = new JPasswordField(12);
    //2
    JTextField jt2_1 = new JTextField(12);
    JTextField jt2_2 = new JTextField(12);
    JTextField jt2_3 = new JTextField(12);

    //按钮设置
    //1
    JButton jb1_1 = new JButton("登录");
    JButton jb1_2 = new JButton("重新输入");
    JButton jb1_3 = new JButton("注册");
    //2
    JButton jb2_1 = new JButton("完成");
    JButton jb2_2 = new JButton("取消");

    //窗口设置
    //1
    JFrame jf1 = new JFrame("登录界面");
    //2
    JFrame jf2 = new JFrame("注册界面");

    //Box盒子设置
    //1
    Box boxLabel,boxText,boxButton,boxUp,baseBox1;
    //2
    Box boxLabel_,boxText_,boxButton_,boxUp_,baseBox2;

    public void ShowUserIneractive(boolean key1, boolean key2){
        //设置布局==============================================
        jf1.setLayout(new FlowLayout());
        jf2.setLayout(new FlowLayout());
        //设置文本框宽、高========================================
        jt1_1.setMaximumSize(new Dimension(2000,1500));
        jt1_2.setMaximumSize(new Dimension(2000,1500));
        //标签入盒==============================================
        boxLabel = Box.createVerticalBox();
        boxLabel.add(lab1_1);
        boxLabel.add(Box.createVerticalStrut(50));
        boxLabel.add(lab1_2);

        boxLabel_ = Box.createVerticalBox();
        boxLabel_.add(lab2_1);
        boxLabel_.add(Box.createVerticalStrut(50));
        boxLabel_.add(lab2_2);
        boxLabel_.add(Box.createVerticalStrut(50));
        boxLabel_.add(lab2_3);

        //文本框入盒=============================================
        boxText = Box.createVerticalBox();
        boxText.add(jt1_1);
        boxText.add(Box.createVerticalStrut(50));
        boxText.add(jt1_2);

        boxText_ = Box.createVerticalBox();
        boxText_.add(jt2_1);
        boxText_.add(Box.createVerticalStrut(50));
        boxText_.add(jt2_2);
        boxText_.add(Box.createVerticalStrut(50));
        boxText_.add(jt2_3);

        //按钮入盒==============================================
        boxButton = Box.createHorizontalBox();
        boxButton.add(jb1_1);
        boxButton.add(Box.createHorizontalStrut(50));
        boxButton.add(jb1_2);
        boxButton.add(Box.createHorizontalStrut(50));
        boxButton.add(jb1_3);

        boxButton_ = Box.createHorizontalBox();
        boxButton_.add(jb2_1);
        boxButton_.add(Box.createHorizontalStrut(50));
        boxButton_.add(jb2_2);

        //上半部分入盒===========================================
        boxUp = Box.createHorizontalBox();
        boxUp.add(boxLabel);
        boxUp.add(Box.createHorizontalStrut(50));
        boxUp.add(boxText);

        boxUp_ = Box.createHorizontalBox();
        boxUp_.add(boxLabel_);
        boxUp_.add(Box.createHorizontalStrut(50));
        boxUp_.add(boxText_);

        //整体入盒==============================================
        baseBox1 = Box.createVerticalBox();
        baseBox1.add(boxUp);
        baseBox1.add(Box.createVerticalStrut(50));
        baseBox1.add(boxButton);

        baseBox2 = Box.createVerticalBox();
        baseBox2.add(boxUp_);
        baseBox2.add(Box.createVerticalStrut(50));
        baseBox2.add(boxButton_);
        //添加事件相应============================================
        AListener al = new AListener();
        jb1_3.addActionListener(al);
        jb1_2.addActionListener(al);
        jb1_1.addActionListener(al);

        jt2_1.addActionListener(al);
        jt2_2.addActionListener(al);
        jt2_3.addActionListener(al);

        jb2_1.addActionListener(al);
        jb2_2.addActionListener(al);
        //显示第一个窗口===========================================
        jf1.add(baseBox1);
        jf1.setBounds(600, 600, 500, 400);
        jf1.setVisible(key1);
        jf1.setDefaultCloseOperation(EXIT_ON_CLOSE);//按x退出
        //显示第二个窗口
        jf2.add(baseBox2);
        jf2.setBounds(600, 600, 500, 400);
        jf2.setVisible(key2);
        jf2.setDefaultCloseOperation(EXIT_ON_CLOSE);//按x退出
    }
    //编写事件响应类===============================================
    class AListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getActionCommand().equals("注册"))
            {
                lab2_1.setForeground(Color.BLACK);

                lab2_1.setText("请输入新的用户名：");

                lab2_2.setForeground(Color.BLACK);

                lab2_2.setText("请输入新用户密码：");

                key1 = false;

                key2 = true;

                jf1.setVisible(key1);

                jf2.setVisible(key2);

                jf1.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口1
            }

            else if(e.getActionCommand().equals("重新输入"))
            {
                jt1_1.setText(" ");

                jt1_2.setText(" ");

                lab1_2.setForeground(Color.BLACK);

                lab1_2.setText("密码:");
            }
            //注册内容响应
            else if(e.getActionCommand().equals("完成"))
            {
                int iscontinue = 1;

                String name = jt2_1.getText();

                String password1 = jt2_2.getText();

                String password2 = jt2_3.getText();

                //判断用户名是否已经存在
                //判断用户名是否重复
                try {
                    FileReader inOne = new FileReader("D:\\username.txt");

                    BufferedReader inTwo = new BufferedReader(inOne);

                    String s = null;

                    String []newName = new String[100];

                    int i = 0;

                    int pos = 0;

                    while((s=inTwo.readLine())!=null)
                    {
                        if(s.equals(name))//发现相同的名字给出提示
                        {
                            lab2_1.setForeground(Color.RED);

                            lab2_1.setText("该用户已存在 ");

                            iscontinue = 0;

                            break;
                        }

                    }
                }
                catch(IOException q)
                {

                    q.printStackTrace();

                }
                //读取文件信息

                if(iscontinue == 1)
                {
                    //判断密码
                    if(password1.length() >= 6)
                    {
                        lab2_2.setForeground(Color.BLACK);

                        lab2_2.setText("请输入新用户密码: ");

                        if(password1.equals(password2))//判断两次密码是否一致
                        {
                            key2 = false;

                            jf2.setVisible(key2);

                            jf2.setDefaultCloseOperation(EXIT_ON_CLOSE);//密码正确关闭窗口2

                            //将用户信息存入文件
                            //创建用户姓名文件

                            File file = new File("D://username.txt");

                            FileOutputStream fos = null;

                            OutputStreamWriter osw = null;

                            try {
                                if(!file.exists())
                                {
                                    boolean hasFile = file.createNewFile();

                                    if(hasFile)
                                    {
                                        System.out.println("file not exists, create new file");
                                    }

                                    fos = new FileOutputStream(file);

                                }else
                                {

                                    System.out.println("file exists");

                                    fos = new FileOutputStream(file, true);

                                }

                                osw = new OutputStreamWriter(fos,"gbk");

                                osw.write(name);

                                osw.write("\r\n");

                            }catch(Exception q){

                                q.printStackTrace();

                            }finally {

                                try
                                {
                                    if(osw!=null)
                                    {
                                        osw.close();
                                    }
                                }catch(IOException q)
                                {
                                    q.printStackTrace();
                                }

                                try
                                {
                                    if(fos!=null)
                                    {
                                        fos.close();
                                    }
                                }catch(IOException q)
                                {
                                    q.printStackTrace();
                                }
                            }

                            //创建用户密码文件

                            File file2 = new File("D://userpassword.txt");

                            FileOutputStream fos2 = null;

                            OutputStreamWriter osw2 = null;

                            try {

                                if(!file.exists())
                                {
                                    boolean hasFile2 = file2.createNewFile();

                                    if(hasFile2)
                                    {
                                        System.out.println("file not exists, create new file");
                                    }

                                    fos2 = new FileOutputStream(file2);

                                }else
                                {
                                    System.out.println("file exists");

                                    fos2 = new FileOutputStream(file2, true);
                                }

                                osw2 = new OutputStreamWriter(fos2,"gbk");

                                osw2.write(password1);

                                osw2.write("\r\n");

                            }catch(Exception q){

                                q.printStackTrace();

                            }finally {

                                try
                                {

                                    if(osw2!=null)
                                    {
                                        osw2.close();
                                    }

                                }catch(IOException q)
                                {
                                    q.printStackTrace();
                                }

                                try
                                {
                                    if(fos2!=null)
                                    {
                                        fos2.close();
                                    }

                                }catch(IOException q)

                                {
                                    q.printStackTrace();
                                }
                            }
                            //返回登录界面
                            JOptionPane.showMessageDialog(null, "注册成功", null, JOptionPane.INFORMATION_MESSAGE);

                            key1 = true;

                            jf1.setVisible(key1);

                        }

                        else//密码错误提示重新输入

                        {

                            lab2_3.setForeground(Color.RED);

                            lab2_3.setText("密码错误请重新输入");

                        }
                    }

                    else

                    {

                        lab2_2.setForeground(Color.RED);

                        lab2_2.setText("密码至少为六位");

                    }

                }

            }

            else if(e.getActionCommand().equals("取消"))
            {

                //关闭注册界面
                key2 = false;

                jf2.setVisible(key2);

                jf2.setDefaultCloseOperation(EXIT_ON_CLOSE);



                //返回登录界面
                key1 = true;

                jf1.setVisible(key1);

            }
            else if(e.getActionCommand().equals("登录"))
            {

                String Uname = jt1_1.getText();

                String Upassword = jt1_2.getText();

                //1、在username文件里找Uname进行比较判断此用户是否注册过，并记录下用户当前所在行数
                //2、在userpassword文件里找Upassword取相同的行数的字符串与用户输入的字符串作比较判断是否一致，
                //3、一致则登录成功，不一致则登陆失败。
                try {
                    //1、(1)将文件中的姓名信息存入数组
                    FileReader inOne = new FileReader("D:\\username.txt");

                    BufferedReader inTwo = new BufferedReader(inOne);

                    String s = null;

                    String []strName = new String[100];

                    int i = 0;

                    int pos = 0;

                    while((s=inTwo.readLine())!=null)
                    {
                        if(s.equals(Uname))
                            pos = i;//定位用户名所在行数

                        strName[i] = s;

                        i++;
                    }

                    inOne.close();

                    inTwo.close();

                    //1、(1)将文件中的密码信息存入数组
                    FileReader inOne2 = new FileReader("D:\\userpassword.txt");

                    BufferedReader inTwo2 = new BufferedReader(inOne2);

                    String s2 = null;

                    String []strPassword = new String[100];

                    int j = 0;

                    while((s2=inTwo2.readLine())!=null)
                    {
                        strPassword[j] = s2;

                        j++;

                    }

                    inOne2.close();

                    inTwo2.close();

                    //3、判断用户名的密码是否正确

                    if(strPassword[pos].equals(Upassword)&&strName[pos].equals(Uname))
                    {
                        key1 = false;

                        jf1.setVisible(key1);

                        jf1.setDefaultCloseOperation(EXIT_ON_CLOSE);

                        JOptionPane.showMessageDialog(null, "登录成功", null, JOptionPane.INFORMATION_MESSAGE);
                    }
                    else
                    {
                        lab1_2.setForeground(Color.RED);

                        lab1_2.setText("密码错误请重新输入");
                    }


                }catch(IOException e1)
                {
                    e1.printStackTrace();
                }

            }
        }
    }

    public String toString()
    {
        return this+" ";
    }

    //设置选择页面

    boolean key1,key2;

    public boolean useKey1()
    {
        return true;
    }

    public boolean useKey2()
    {
        return true;
    }

    public boolean noUseKey1()
    {
        return false;
    }

    public boolean noUseKey2()
    {
        return false;
    }




    //主方法
    public static void main(String[] args)
    {

        UserInteractive UI = new UserInteractive();
        //程序开始默认打开第一个界面
        UI.ShowUserIneractive(UI.useKey1(), UI.key2);

    }
}
